package com.api.oa.utils;

public class CoordinateUtil {

    private static final  double EARTH_RADIUS = 6378137;//赤道半径(单位m)

    /**
     * 地球半径
     */
    private static double EarthRadius = 6378.137;

    /**
     * 计算两个坐标点之间的距离
     * Add by 成长的小猪（Jason.Song） on 2017/11/01
     * http://blog.csdn.net/jasonsong2008
     *
     * @param firstLatitude   第一个坐标的纬度
     * @param firstLongitude  第一个坐标的经度
     * @param secondLatitude  第二个坐标的纬度
     * @param secondLongitude 第二个坐标的经度
     * @return 返回两点之间的距离，单位：公里/千米
     */
    public static double getDistance(double firstLatitude, double firstLongitude,
                                     double secondLatitude, double secondLongitude) {
        double firstRadLat = rad(firstLatitude);
        double firstRadLng = rad(firstLongitude);
        double secondRadLat = rad(secondLatitude);
        double secondRadLng = rad(secondLongitude);

        double a = firstRadLat - secondRadLat;
        double b = firstRadLng - secondRadLng;
        double cal = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(firstRadLat)
                * Math.cos(secondRadLat) * Math.pow(Math.sin(b / 2), 2))) * EarthRadius;
        double result = Math.round(cal * 10000d) / 10000d;
        return result;
    }



    /**
     * 转化为弧度(rad)
     * */
    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }

    /**
     * 基于余弦定理求两经纬度距离
     * @param lon1 第一点的精度
     * @param lat1 第一点的纬度
     * @param lon2 第二点的精度
     * @param lat2 第二点的纬度
     * @return 返回的距离，单位km
     * */
    public static double latLngDist(double lon1, double lat1,double lon2, double lat2) {
//        double radLat1 = rad(lat1);
//        double radLat2 = rad(lat2);
//
//        double radLon1 = rad(lon1);
//        double radLon2 = rad(lon2);
//
//        if (radLat1 < 0)
//            radLat1 = Math.PI / 2 + Math.abs(radLat1);// south
//        if (radLat1 > 0)
//            radLat1 = Math.PI / 2 - Math.abs(radLat1);// north
//        if (radLon1 < 0)
//            radLon1 = Math.PI * 2 - Math.abs(radLon1);// west
//        if (radLat2 < 0)
//            radLat2 = Math.PI / 2 + Math.abs(radLat2);// south
//        if (radLat2 > 0)
//            radLat2 = Math.PI / 2 - Math.abs(radLat2);// north
//        if (radLon2 < 0)
//            radLon2 = Math.PI * 2 - Math.abs(radLon2);// west
//        double x1 = EARTH_RADIUS * Math.cos(radLon1) * Math.sin(radLat1);
//        double y1 = EARTH_RADIUS * Math.sin(radLon1) * Math.sin(radLat1);
//        double z1 = EARTH_RADIUS * Math.cos(radLat1);
//
//        double x2 = EARTH_RADIUS * Math.cos(radLon2) * Math.sin(radLat2);
//        double y2 = EARTH_RADIUS * Math.sin(radLon2) * Math.sin(radLat2);
//        double z2 = EARTH_RADIUS * Math.cos(radLat2);
//
//        double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)+ (z1 - z2) * (z1 - z2));
//        //余弦定理求夹角
//        double theta = Math.acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d) / (2 * EARTH_RADIUS * EARTH_RADIUS));
//        double dist = theta * EARTH_RADIUS;
        return getDistance(lat1,lon1,lat2,lon2) * 1000;
    }


}
